package gui;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;

import eolympics.Game;

public class MainFrame
        extends JFrame
        implements IMainFrameScreenController
{
    private static final long   serialVersionUID                  = 4714864527745266449L;
    private static final String TITLE                             = "Eolympics";
    private static final float  INITIAL_FRACTION_OF_SCREEN_WIDTH  = 3F / 4F;
    private static final float  INITIAL_FRACTION_OF_SCREEN_HEIGHT = 3F / 4F;

    private final Game          mGame;

    private StartGameScreen     mStartGameScreen;
    private HomeScreen          mHomeScreen;

    public enum Screens
    {
        START_GAME_SCREEN,
        HOME_SCREEN;
    }

    public MainFrame(final Game pGame)
    {
        super(TITLE);

        mGame = pGame;

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int initialWidth = (int) (screenSize.width * INITIAL_FRACTION_OF_SCREEN_WIDTH);
        int initialHeight = (int) (screenSize.height * INITIAL_FRACTION_OF_SCREEN_HEIGHT);
        setBounds(0, 0, initialWidth, initialHeight);

        mStartGameScreen = new StartGameScreen(this, pGame);
        mHomeScreen = new HomeScreen(this, pGame);

        setLocationRelativeTo(null);

        changeScreen(Screens.HOME_SCREEN);
    }

    @Override
    public void changeScreen( final Screens pNewScreen )
    {
        ScreenParent newScreen = mHomeScreen;

        if( pNewScreen == Screens.START_GAME_SCREEN )
        {
            newScreen = mStartGameScreen;
        }

        mGame.setClientEventsListener(newScreen);

        newScreen.init();

        getContentPane().removeAll();
        getContentPane().add(newScreen);
        getContentPane().revalidate();

        repaint();
    }

}
